package com.jmp.autocofiguration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger", value = {"enable"}, havingValue = "true")
public class SwaggerConfig {

    @Bean
    @Primary
    public ServiceModelToSwagger2Mapper serviceModelToSwagger2Mapper() {
        return new ServiceModelToSwagger2MapperImplExt();
    }

    protected Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .globalOperationParameters(globalOperationParameters())
                .select()
                ////为当前包路径
                .apis(RequestHandlerSelectors.basePackage("one.stand.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    protected Docket createRestApi(String packageName, String groupName) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName)
                .apiInfo(apiInfo())
                .globalOperationParameters(globalOperationParameters())
                .select()
                ////为当前包路径
                .apis(RequestHandlerSelectors.basePackage(packageName))
                .paths(PathSelectors.any())
                .build();
    }

    protected ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Api")
                .version("1.0")
                .build();
    }

    protected List<Parameter> globalOperationParameters() {
        List<Parameter> pars = new ArrayList<Parameter>();
        Parameter paraToken = new ParameterBuilder()
                .name("token")
                .description("user token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build(); //header中的token参数非必填，传空也可以
        Parameter paraAuth = new ParameterBuilder()
                .name(HttpHeaders.AUTHORIZATION)
                .description("user Authorization")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build(); //header中的token参数非必填，传空也可以
        pars.add(paraToken);    //根据每个方法名也知道当前方法在设置什么参
        pars.add(paraAuth);    //根据每个方法名也知道当前方法在设置什么参
        return pars;
    }
}
